﻿using QFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace projectgame
{
    public partial class PlantChineseCabbage : ViewController, IPlant
    {
        public int XCell;
        public int YCell;

        public PlantStates mState = PlantStates.Seed;
        public PlantStates State => mState;

        public int RipeDay { get; private set; }
        public void SetState(PlantStates newState)//设置植物状态的方法
        {
            if (newState != mState) //如果所传状态不等于当前植物状态，则继续执行
            {
                if (mState == PlantStates.Small && newState == PlantStates.Ripe)
                {
                    RipeDay = Global.Days.Value;
                }
                mState = newState;//令当前植物状态等于所传状态
                if (newState == PlantStates.Small)//如果所传状态为Small
                {
                    GetComponent<SpriteRenderer>().sprite = ResController.Instance.SmallPlantChineseCabbageSprite;//将植物sprite设置为SmallPlantSprite
                }
                else if (newState == PlantStates.Ripe)//如果所传状态为Ripe
                {
                    GetComponent<SpriteRenderer>().sprite = ResController.Instance.RipeChineseCabbageSprite;//将植物sprite设置为RipeSprite
                }
                else if (newState == PlantStates.Seed)//如果所传状态为Seed
                {
                    GetComponent<SpriteRenderer>().sprite = ResController.Instance.SeedChineseCabbageSprite;//将植物sprite设置为SeedSprite
                }
                else if (newState == PlantStates.Old)//如果所传状态为Seed
                {
                    GetComponent<SpriteRenderer>().sprite = ResController.Instance.OldSprite;//将植物sprite设置为SeedSprite
                }
                FindObjectOfType<GridController>().ShowGrid[XCell, YCell].PlantState = newState;//获取植物所处地块的SoilData，并将其PlantState设置为所传参数
                FindObjectOfType<GridController>().ShowGrid[XCell, YCell].PlantState = newState;//获取植物所处地块的SoilData，并将其PlantState设置为所传参数


            }
        }
        void Start()
        {
            // Code Here
        }
        private int mSeedStateDay = 0;
        private int mSmallStateDay = 0;

        public void Grow(SoilData soilData)
        {
            if (State == PlantStates.Seed)
            {
                if (soilData.Watered)
                {
                    mSeedStateDay++;
                    if (mSeedStateDay == 2)
                    {
                        SetState(PlantStates.Small); 
                    }
                }
            }
            else if (State == PlantStates.Small)
            {
                if (soilData.Watered)
                {
                    mSmallStateDay++;
                    if (mSmallStateDay == 2)
                    {
                        SetState(PlantStates.Ripe);
                    }
                }
            }
        }

        public GameObject GameObject => gameObject;
    }
}
    
    

